package caseManagerBusiness;

import com.alibaba.fastjson.JSONObject;
import easyDAO.SqlDispatch;
import impl.TestServerBusinessImpl;
import models.GetRequestInputStream;
import models.MyConfig;
import models.ReturnJson;
import models.SqlReturnModel;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * Created by yangxianming on 2019/1/2.
 * 更新测试结果
 */
public class SaveReport implements TestServerBusinessImpl {
    @Override
    public String doIt(HttpServletRequest request, MyConfig myConfig) {
        //初始化返回对象。读取请求json
        ReturnJson returnJson = new ReturnJson();
        JSONObject resultInfo = GetRequestInputStream.getJson(request);
        SqlReturnModel sqlReturnModel = new SqlReturnModel();
        SqlDispatch sqlDispatch = new SqlDispatch(myConfig);
        boolean uiSaveReport = myConfig.getPropertiesConfig().getProperties().getProperty("uiSaveReport").equals("true");
        String result = resultInfo.getString("result");
        String caseId = resultInfo.getString("caseId");
        String log = resultInfo.getString("log");
        log = Objects.equals(log, null) ? "" : log;
        String type = resultInfo.getString("type");
        String sqlUpdateSet = "";

        //python的接口测试报告
        if (Objects.equals(type, "3")){
            String testResult = resultInfo.getString("testResult");
            String expectResult = resultInfo.getString("expectResult");

            //判断一下 log属性值长度，超长就就用too more替换
            if (testResult.length() > 50000){
                resultInfo.put("testResult", "测试记录数据过长，请手动执行查看返回数据");
            }
            //batchnumber 这个批次数据暂时不用
            testResult = repSign(testResult);
            expectResult = repSign(expectResult);
            sqlUpdateSet = sqlUpdateSet + " `testResult`=\'" + testResult + "\',";
            sqlUpdateSet = sqlUpdateSet + " `expectResult`=\'" + expectResult + "\',";
            sqlUpdateSet = sqlUpdateSet + " `result`=\'" + result + "\',";
        }
        //ui测试报告的保存，如果需要特殊处理
        if (Objects.equals(type, "1") && uiSaveReport){ //ui自动化测试报告
            sqlUpdateSet = sqlUpdateSet + " result='" + result + "',type=" + type + ",";
        }

        //根据caseId先查询数据，有-更新，没有-插入
        //SELECT caseId,success,fail FROM testserver.testreport WHERE caseId=2
        String sqlWord = "SELECT caseId,success,fail FROM testserver.testreport WHERE caseId="+caseId;
        SqlReturnModel sqlReturnModel1 = sqlDispatch.selectWord(sqlWord);
        if (Objects.equals(sqlReturnModel1.getSelectReturnData(), null) || sqlReturnModel1.getSelectReturnData().size() < 1){
            //插入，

            JSONObject insertData = new JSONObject();
            insertData.put("caseId", caseId);
            insertData.put("result", result);
            if (Objects.equals(result, "1")){
                insertData.put("success", 1);
                insertData.put("fail", 0);
            }else {
                insertData.put("success", 0);
                insertData.put("fail", 1);
            }
            insertData.put("log", log);
            insertData.put("type", type);
            sqlReturnModel = sqlDispatch.insert("testreport", insertData);
        }else {
            //更新 只更新success或者fail数据，这两个字段是累加记录
            //2022-12-22 12:53:34
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String nowTime = sdf1.format(new Date());
            int intSuccess = Integer.parseInt(sqlReturnModel1.getSelectReturnData().get(0).get(1));
            int intFail = Integer.parseInt(sqlReturnModel1.getSelectReturnData().get(0).get(2));
            log = repSign(log);
            String sqlUpdate = "UPDATE testserver.testreport SET ";
            sqlUpdateSet = sqlUpdateSet + " log='" + log + "'" +", updatetime=\""+nowTime+"\", ";
            String sqlUpdateWhere = " WHERE caseId=" + caseId;
            if (Objects.equals(result, "1")){
                //成功直接用例susess加1
                sqlUpdateSet = sqlUpdateSet + "success=" + ++intSuccess;
            }else {
                sqlUpdateSet = sqlUpdateSet + "fail=" + ++intFail;
            }
            String updateSql = sqlUpdate + sqlUpdateSet + sqlUpdateWhere;
            System.out.println(updateSql);
            sqlReturnModel = sqlDispatch.update(updateSql);
        }

        returnJson.setApiStatus((sqlReturnModel.getIsSuccess()) ? 1 : 0);
        returnJson.setErrorMsg(sqlReturnModel.getFailingReason());
        return returnJson.toString();
    }

    private String repSign(String s){
        s = s.replace("'", "\\'");
        return s.replace("\"", "\\\"");
    }
}
